package com.wy.allthingdone.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.wy.allthingdone.R;

import org.xutils.view.annotation.ViewInject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class TextReaderActivity extends BaseActivity {
    private String textDir = null;
    @ViewInject(R.id.text)
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_text_reader);
        super.onCreate(savedInstanceState);
        if (getIntent().hasExtra("data")) {
            textDir = getIntent().getStringExtra("data");
        }
        if (textDir == null) {
            new Throwable("文件为空");
        }
        text.setText(convertCodeAndGetText(textDir));
//        File file = new File(textDir);
//        StringBuffer sb = new StringBuffer();
//        try {
        // 字符流读取文件数据
//            BufferedReader br = new BufferedReader(new FileReader(file));
//            String line;
//            while ((line = br.readLine()) != null) {
//                sb.append(line);
//            }
//            br.close();
//            // 字节流读取文件数据
//            FileInputStream fis = new FileInputStream(file);
//            int n = 1024;
//            byte buffer[] = new byte[n];
//            while ((fis.read(buffer, 0, n) != -1) && (n > 0)) {
//                String temp1 = EncodingUtils.getString(buffer, "GB2312");
//                sb.append(new String(buffer, "GB2312"));
//            }
//            fis.close();

//            text.setText(sb.toString());
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

    public String convertCodeAndGetText(String str_filepath) {// 转码

        File file = new File(str_filepath);
        BufferedReader reader;
        String text = "";
        try {
            // FileReader f_reader = new FileReader(file);
            // BufferedReader reader = new BufferedReader(f_reader);
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fis);
            in.mark(4);
            byte[] first3bytes = new byte[3];
            in.read(first3bytes);//找到文档的前三个字节并自动判断文档类型。
            in.reset();
            if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
                    && first3bytes[2] == (byte) 0xBF) {// utf-8

                reader = new BufferedReader(new InputStreamReader(in, "utf-8"));

            } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) {

                reader = new BufferedReader(new InputStreamReader(in, "unicode"));
            } else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) {

                reader = new BufferedReader(new InputStreamReader(in, "utf-16be"));
            } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) {

                reader = new BufferedReader(new InputStreamReader(in, "utf-16le"));
            } else {
                reader = new BufferedReader(new InputStreamReader(in, "GBK"));
            }
            String str = reader.readLine();

            while (str != null) {//读取的时候进行判断
                text = text + str + "\n";
                str = reader.readLine();

            }
            reader.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text;
    }
}
